package com.woniu.service.impl;

import com.woniu.dao.ClazzDao;
import com.woniu.dao.LessonDao;
import com.woniu.pojo.*;
import com.woniu.service.ClazzService;
import com.woniu.service.LessonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
@Transactional
public class ClazzServiceImpl implements ClazzService {
    @Resource
    private ClazzDao clazzDao;
    @Resource
    private LessonDao lessonDao;
    @Override
    public List<Clazz> findAllClazz() {
        return clazzDao.selectByExample(null);
    }

    @Override
    public void addClazz(Clazz clazz) {

        clazzDao.insertSelective(clazz);
    }

    @Override
    public void deleteClazzByCid(Integer cid) {
        clazzDao.deleteByPrimaryKey(cid);
    }

    @Override
    public Clazz toUpdateByCid(Integer cid) {
        return clazzDao.selectByPrimaryKey(cid);
    }

    @Override
    public void updateByCid(Clazz clazz) {
        clazzDao.updateByPrimaryKeySelective(clazz);
    }



    @Override
    public List<Clazz> findbiyeAll() {
        ClazzExample clazzExample = new ClazzExample();
        ClazzExample.Criteria criteria = clazzExample.createCriteria();
        criteria.andCstateEqualTo("已结课");
        List<Clazz> clazzList = clazzDao.selectByExample(clazzExample);
        return clazzList;
    }

    @Override
    public List<Clazz> findClazzByPage(PageBean pageBean, Clazz clazz) {
        List<Clazz> clazzListBe=findAll();
        Date date=new Date();
        for (Clazz clazz1 : clazzListBe) {
          Date begin= clazz1.getBegin();
          Date end= clazz1.getEnd();

            if (begin.after(date)){clazz1.setCstate("未开班");}
            if (end.after(date)&&begin.before(date)){clazz1.setCstate("正常");}
            if (end.before(date)){clazz1.setCstate("已结课");}
            clazzDao.updateByPrimaryKeySelective(clazz1);
        }

        ClazzExample clazzExample=new ClazzExample();
        ClazzExample.Criteria criteria = clazzExample.createCriteria();
        if(clazz.getCid()!=null&&!clazz.getCid().equals("")){
            criteria.andCidEqualTo(clazz.getCid());
        }
        if(clazz.getUid()!=null&&!clazz.getUid().equals("")){
            criteria.andUidEqualTo(clazz.getUid());
        }
        if(clazz.getCname()!=null&&!clazz.getCname().equals("")){
            criteria.andCnameLike("%"+clazz.getCname()+"%");
        }
        if(clazz.getCstate()!=null&&!clazz.getCstate().equals("")){
            criteria.andCstateEqualTo(clazz.getCstate());
        }

        Long countRow= clazzDao.countByExample(clazzExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage=pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        clazzExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        clazzExample.setLimit(pageBean.getPageSize());
        clazzExample.setOrderByClause("cid DESC");
        List<Clazz> clazzList=clazzDao.selectByExample(clazzExample);
        return clazzList;

    }

    /**
     * 所有班级排序查询
     * @return
     */
    @Override
    public Clazz beyetimeDo() {
        ClazzExample clazzExample = new ClazzExample();
        clazzExample.setOrderByClause("end ASC");
        List<Clazz> clazzList = clazzDao.selectByExample(clazzExample);
        //获取第一个和最后一个end值
        Clazz clazz = new Clazz();
        clazz.setBegin(clazzList.get(0).getEnd());
        clazz.setEnd(clazzList.get(clazzList.size()-1).getEnd());
        return clazz;
    }

    /**
     * 查询所有的班级
     * @return
     */
    @Override
    public List<Clazz> findAll() {
        ClazzExample clazzExample = new ClazzExample();
        clazzExample.createCriteria().andCstateEqualTo("已结课");
        List<Clazz> clazzList = clazzDao.selectByExample(clazzExample);
        return clazzList;
    }
    @Override
    public List<Clazz> tclazzList(PageBean pageBean,Clazz clazz,User user) {
        ClazzExample clazzExample=new ClazzExample();
        ClazzExample.Criteria clazzExampleCriteria = clazzExample.createCriteria();
        List<Clazz> clazzList=new ArrayList<>();
        List<Lesson> lessonList=lessonDao.selectByExample(null);
         List<Integer> cidList=new ArrayList<>();
        for (Lesson lesson : lessonList) {
            if(lesson.getName().equals(user.getUname())){
                cidList.add(lesson.getCid());
            }
        }
        if(clazz.getCstate()!=null&&!clazz.getCstate().equals("")){
            clazzExampleCriteria.andCstateEqualTo(clazz.getCstate());
        }
        if(clazz.getCname()!=null&&!clazz.getCname().equals("")){
            clazzExampleCriteria.andCnameLike("%"+clazz.getCname()+"%");
        }
        clazzExampleCriteria.andCidIn(cidList);

        Long countRow= clazzDao.countByExample(clazzExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage=pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        clazzExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        clazzExample.setLimit(pageBean.getPageSize());
        clazzList=clazzDao.selectByExample(clazzExample);
        for (Clazz clazz1 : clazzList) {
            clazz1.setTname(user.getUname());
        }
        return  clazzList;
    }

    @Override
    public List<Clazz> htclazzList(PageBean pageBean,Clazz clazz,User user) {
        ClazzExample clazzExample=new ClazzExample();
        ClazzExample.Criteria clazzExampleCriteria = clazzExample.createCriteria();
        List<Clazz> clazzList=new ArrayList<>();

        if(clazz.getCstate()!=null&&!clazz.getCstate().equals("")){
            clazzExampleCriteria.andCstateEqualTo(clazz.getCstate());
        }
        if(clazz.getCname()!=null&&!clazz.getCname().equals("")){
            clazzExampleCriteria.andCnameLike("%"+clazz.getCname()+"%");
        }
        clazzExampleCriteria.andUidEqualTo(user.getUid());

        Long countRow= clazzDao.countByExample(clazzExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage=pageBean.getCountRow()%pageBean.getPageSize()==0?pageBean.getCountRow()/pageBean.getPageSize():pageBean.getCountRow()/pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);
        clazzExample.setOffset((long)((pageBean.getCurrentPage()-1)*pageBean.getPageSize()));
        clazzExample.setLimit(pageBean.getPageSize());
        clazzList=clazzDao.selectByExample(clazzExample);
        if(clazzList.size()!=0){
        List<Lesson> lessonList=new ArrayList<>();
        LessonExample lessonExample=new LessonExample();
        LessonExample.Criteria lessonExampleCriteria = lessonExample.createCriteria();
        List<Integer> cidList=new ArrayList<>();
        for (Clazz clazz1 : clazzList) {
            cidList.add(clazz1.getCid());
        }
        lessonExampleCriteria.andCidIn(cidList);
        lessonList= lessonDao.selectByExample(lessonExample);
        for (Lesson lesson : lessonList) {
            for (Clazz clazz1 : clazzList) {
                if(lesson.getCid()==clazz1.getCid()){
                clazz1.setTname(lesson.getName());}
            }
        }
        }
        return  clazzList;
    }

}
